package 五月打卡;

/*
 * @author  YxinMiracle
 * @date  2022-05-03 8:56
 * @Gitee: https://gitee.com/yxinmiracle
 */

import com.sun.deploy.util.StringUtils;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import javafx.util.Pair;

import java.util.*;

public class 重新排列日志文件 {
    public String[] reorderLogFiles(String[] logs) {
        List<String> list = new ArrayList<>();
        PriorityQueue<Pair<Pair<String, String>, Integer>> maxHeap = new PriorityQueue<>(new Comparator<Pair<Pair<String, String>, Integer>>() {
            @Override
            public int compare(Pair<Pair<String, String>, Integer> o1, Pair<Pair<String, String>, Integer> o2) {
                Pair<String, String> o1Key = o1.getKey();
                Pair<String, String> o2Key = o2.getKey();
                if (o1Key.getKey().compareTo(o2Key.getKey()) == 0){ // 相同
                    return o1Key.getValue().compareTo(o2Key.getValue());
                }else {
                    return o1Key.getKey().compareTo(o2Key.getKey());
                }
            }
        });
        for (int i = 0; i < logs.length; i++) {
            String[] s = logs[i].split(" ");
            if (s[s.length-1].chars().allMatch(Character::isDigit)) list.add(logs[i]); // 是数字
            else {
                StringBuilder sb = new StringBuilder();
                for (int j = 1; j < s.length; j++) sb.append(s[j]+" ");
                String tail = sb.toString();
                String head = s[0];
                maxHeap.add(new Pair<>(new Pair<>(tail,head),i));
            }
        }
        int index = 0;
        String[] result = new String[logs.length];
        while (!maxHeap.isEmpty()){
            result[index++] = logs[maxHeap.poll().getValue()];
        }
        for (String s : list) {
            result[index++] = s;
        }
        System.out.println(Arrays.toString(result));
        return result;
    }

    public static void main(String[] args) { // can < zero < 0
        String s1 = "kvr";
        String s2 = "so";
        System.out.println(s2.compareTo(s1));
        System.out.println(new 重新排列日志文件().reorderLogFiles(new String[]{"j mo", "5 m w", "g 07", "o 2 0", "t q h"}));
    }
}
